/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xmlopdracht2.apachefop;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import java.io.OutputStream;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;

/**
 *
 * @author Job
 */
public class PdfCreator
{

  public static void CreatePDF(String StudentNr, File xmlFile, String outputPath) throws IOException
  {
    String xsltPath = "src/xmlopdracht2/resources/PDFTemplate.xsl";

    try
    {
      // Setup input and output files
      File xmlfile = xmlFile;
      File xsltfile = new File(xsltPath);

      //Nieuwe FopFactory en useragend aanmaken
      FopFactory fopFactory = FopFactory.newInstance();
      FOUserAgent foUserAgent = fopFactory.newFOUserAgent();

      //Configuratie foUserAgent
      foUserAgent.setTitle("Voortgangsrapportage student " + StudentNr);
      foUserAgent.setCreator("StudentenAdministratie V1.0");
      foUserAgent.setCreationDate(new Date(System.currentTimeMillis()));

      OutputStream out = null;
      //Output instellen
      try
      {
        out = new java.io.FileOutputStream(outputPath);
        out = new java.io.BufferedOutputStream(out);
      } catch (Exception e)
      {
        JOptionPane.showMessageDialog(null,
                "Er is een fout opgetreden bij het aanmaken van het pdfbestand\n" +
                "Opgegeven pad:" + outputPath + "\n" +
                "Foutbericht: " + e.getLocalizedMessage(),
                "",
                JOptionPane.ERROR_MESSAGE);
        return;
      }

      try
      {
        // Construct fop with desired output format
        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);

        // Setup XSLT
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));
        transformer.setParameter("studentNr", StudentNr);
        // Set the value of a <param> in the stylesheet
        transformer.setParameter("versionParam", "2.0");

        // Setup input for XSLT transformation
        Source src = new StreamSource(xmlfile);

        // Resulting SAX events (the generated FO) must be piped through to FOP
        Result res = new SAXResult(fop.getDefaultHandler());

        // Start XSLT transformation and FOP processing
        transformer.transform(src, res);

        JOptionPane.showMessageDialog(null, "PDF opslaan gelukt.", "", JOptionPane.INFORMATION_MESSAGE);
      }
      catch (Exception ex)
      {
        JOptionPane.showMessageDialog(null,
                "Fout bij het verwerken van het document.\n" +
                "Fout: " + ex.getLocalizedMessage(),
                "",
                JOptionPane.ERROR_MESSAGE);
      } finally
      {
        out.close();
      }
    } finally
    {
    }
  }
}
